##### Results Tables A7 and A8 script #####
rm(list=ls());gc();gc();gc();gc();gc();gc();gc();gc()


library(gt)
library(Hmisc)
library(tidyverse)
library(dplyr)
library(hrbrthemes)
library(lfe)
library(stargazer)

here::i_am("Scripts/TabA7A8Script.R")

library(here)

#### TABLE A7 - Donation Level ####

finall<-readRDS(here("Data","descripcontribswgender.rds"))

#Combine statewides that aren't governor
sws<-c("ATTORNEY GENERAL","AUDITOR", "COMMISSIONER OF AGRICULTURE", "SECRETARY OF STATE", "SUPREME COURT", "TREASURER" )

finall$cOFFICE<-ifelse(finall$cOFFICE %in% sws, "STATEWIDE", finall$cOFFICE)


#Do these attributes predict different diets of event vs non-event fundraising 
#(state, time, gender, incumbency, office sought, party) 

#Factor these
finall$statefac<-factor(finall$cSTATE)
finall$timefac<-factor(finall$eleccycnum)
finall$genderfac<-factor(finall$cand_gender)
finall$officefac<-factor(finall$cOFFICE)
finall$officefac<-relevel(finall$officefac, ref="HOUSE")

#Remove third parties
finall<-finall %>%
  filter(party3 %in% c("DEM", "REP"))

finall$partyfac<-factor(finall$party3)


finall$incfac<-factor(finall$cINCUMBENCYSTATUS2)
finall$year<-factor(as.numeric(substr(finall$cDATE,1,4)))

finall<-finall%>%
  filter(!is.na(cand_gender), cINCUMBENCYSTATUS2!="", cINCUMBENCYSTATUS2!="Unknown", eleccycnum!=6)


mod<-lm(fundraiser~statefac+timefac+year+genderfac+officefac+partyfac+incfac, data=finall)

summary(mod)


stargazer(mod, intercept.bottom = FALSE,single.row = TRUE,dep.var.labels=c("Probability of Donation Stemming from Event"),
          out=here("Results", "TabA7.tex"),covariate.labels=c("Intercept","Michigan","Ohio","West Virginia", "Election Cycle 2", "Election Cycle 3", "Election Cycle 4", "Election Cycle 5", "Male Candidate", 
                             "Governor", "Senate", "Other Statewide", "Republican Candidate", "Non-Incumbent"),
          omit.stat=c("LL","ser","f"), no.space=TRUE,omit="year",
          add.lines=list(c('Year Fixed Effects', 'Yes')))




#### TABLE A8 - Candidacy Level ####

#at the candidacy level, identify whether there are any fundraiser 
#donations collected, then slice the 
#top 1 off in each year (should be same on all we care about)

finallcand<-finall%>%
  group_by(Candidate2Office2Year)%>%
  mutate(yesevents=ifelse(sum(fundraiser)>0,1,0))%>%
  ungroup()%>%
  group_by(Candidate2Office2Year, year)%>%
  slice_head()

finallcand<-finallcand%>%
  filter(!is.na(cand_gender), cINCUMBENCYSTATUS2!="", cINCUMBENCYSTATUS2!="Unknown", eleccycnum!=6)


mod2<-lm(yesevents~statefac+timefac+year+genderfac+officefac+partyfac+incfac, data=finallcand)

summary(mod2)


stargazer(mod2, intercept.bottom = FALSE,single.row = TRUE,dep.var.labels=c("Probability of Candidacy Hosting Event"),
          out=here("Results", "TabA8.tex"),covariate.labels=c("Intercept","Michigan","Ohio","West Virginia", "Election Cycle 2", "Election Cycle 3", "Election Cycle 4", "Election Cycle 5", "Male Candidate", 
                             "Governor", "Senate", "Other Statewide", "Republican Candidate", "Non-Incumbent"),
          omit.stat=c("LL","ser","f"), no.space=TRUE,omit="year",
          add.lines=list(c('Year Fixed Effects', 'Yes')))



